<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Clouds;
use app\admin\model\Areas;
use app\admin\model\CloudItems;
class Cloud extends Common{
    public function _initialize()
    {
        parent::_initialize();
        $this->Clouds = new Clouds;
        
    }
    public function index($pid=null){
        $this->assign('pid',$pid);
        if($pid > 0){
            $name = $this->Clouds->where('id',$pid)->value('name');
        }
        $this->assign('name',$name);
        $this->assign('pid',(int)$pid);
    	return $this->fetch();
    } 	
    public function getLists($page=null,$pid=0,$key=null){
    	if($this->request->isPost()){
            $page = (int)$page > 0 ? $page : 1;
            $map=[];
            if(!empty($key)){
                $map['name|name_en'] = ['like',"%".$key."%"];
            }
            $lists = $this->Clouds->getCloudsList($map);
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists,'count'=>count($lists),'rel'=>1];
        }
    }
    public function edit($id=null,$pid=null){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['name'])){
                return ['code'=>0,'msg'=>lang('Please enter the name')];
            }
            if($id > 0){
                $result = $this->Clouds->isUpdate()->save($data);
            }else{
                $result = $this->Clouds->insert($data);
            }
            if($result === false){
                return ['code'=>0,'msg'=>lang('Data save failed!')];
            }
            return ['code'=>1,'msg'=>lang('The data is saved successfully!')];
        }
        if($id>0){
            $row = $this->Clouds->get($id);
        }
        $this->assign('row',$row);
        
        return $this->fetch('edit');
    }
    /*排序*/
    public function sort(){
        $data = input('post.');
        if($this->Clouds->isUpdate()->save($data)!==false){
            return $result = ['msg' =>lang('message_text_5'), 'code' =>1];
        }else{
            return $result = ['code'=>0,'msg'=>lang('message_text_6')];
        }
    }
    public function editState(){
        $id=input('post.id');
        $status=input('post.status');
        if($this->Clouds->isUpdate()->save(['status'=>$status,'id'=>$id])!==false){
            return ['code'=>1,'msg'=>lang('Modify state success')];
        }else{
            return ['code'=>0,'msg'=>lang('Modify state failure')];
        }
    }
    public function delete($id=null){
        if($id > 0 && $this->Clouds->where('id',$id)->delete()){
            return ['code'=>1,'msg'=>lang('Data has been deleted')];
        }
        return ['code'=>0,'msg'=>lang('Delete data failure')];
    }
    public function icon(){
        $content = file_get_contents("tes.css");
        $arr = preg_match_all("/\.icon(.*):/i", $content, $matches);
        $this->assign('icons',$matches[1]);
        return $this->fetch();
    }
    public function getClouList($pid=null,$cloud_id=null){
        $Clouds = Clouds::getCountry($pid,$cloud_id);
        return ['code'=>1,'msg'=>'','items'=>$Clouds];
    }
    /*获取云端城市数据*/
    public function getCloudCity($country_id=null,$cloud_id=null){
        $citys = Clouds::getCity($cloud_id,$country_id);
        return ['code'=>1,'msg'=>'','items'=>$citys];
    }
    /*获取云端数据中心*/
    public function getCloudItems($country_id=null,$cloud_id=null,$city_id=null){
        $items = Clouds::getItems($cloud_id,$country_id,$city_id);
        return ['code'=>1,'msg'=>'','items'=>$items];
    }
    /*数据中心*/
    public function items($page=null,$key=null){
        if($this->request->isPost()){
            $page = (int)$page > 0 ? $page : 1;
            $map=[];
            if(!empty($key)){
                $map['m.name|a.name|c.name'] = ['like',"%".$key."%"];
            }
            $CloudItems = new CloudItems;
            $lists = $CloudItems->items($page,$map);
               
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
        }
        return $this->fetch();
    }
    /*数据中心*/
    public function itemform($id=null){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['cloud_id'])){
                return ['code'=>0,'msg'=>lang('Please select cloud providers.')];
            }
            if(empty($data['country_id'])){
                return ['code'=>0,'msg'=>lang('Please select area')];
            }
            if(empty($data['city_id'])){
                return ['code'=>0,'msg'=>lang('Please choose the city')];
            }
            if(empty($data['name'])){
                return ['code'=>0,'msg'=>lang('Please enter the data center name.')];
            }

            if($id > 0){
                $result = db('cloud_items')->update($data);
            }else{
                $data['status'] = 1;
                $data['create_time'] = time();
                $result = db('cloud_items')->insert($data);
            }
            if($result){
                return ['code'=>1,'msg'=>lang('The data is saved successfully!')];
            }
            return ['code'=>0,'msg'=>lang('Data save failed!')];
        }
        if($id > 0){
            $row = db('cloud_items')->where(['id'=>$id])->find();
        }
        $this->assign('row',$row);
        $this->assign('areas',Areas::getAreas(0));
        $this->assign('clouds',$this->Clouds->select());
        return $this->fetch();
    }

    public function delItem($id=null){
        if($id > 0 && db('cloud_items')->where('id',$id)->delete()){
            return ['code'=>1,'msg'=>lang('Data has been deleted')];
        }
        return ['code'=>0,'msg'=>lang('Delete data failure')];
    }
    public function editStateItem(){
        $id=input('post.id');
        $status=input('post.status');
        if(db('cloud_items')->update(['status'=>$status,'id'=>$id])!==false){
            return ['code'=>1,'msg'=>lang('Modify state success')];
        }else{
            return ['code'=>0,'msg'=>lang('Modify state failure')];
        }
    }
    public function view($pid=null,$cloud_id=null){
        $this->assign('clouds',$this->Clouds->getClouds());
        return $this->fetch();
    }
}